<?php
class UserAR extends CActiveRecord{
	public static function model($className=__CLASS__){
		return parent::model($className);
	}

	public static function tblName()	{
		return 'ccs8_user';
	}
	
	public function tableName()	{
		return self::tblName();
	}
	
    public static function saveUser($username,$password,$email){
        $regIp = MyUtil::getRemoteAddr();
        $regTime = date('Y-m-d H:i:s');
        $data = array('user_name'=>$username,'password'=>md5($password),'email'=>$email,'reg_ip'=>$regIp,'reg_time'=>$regTime);
        DBUtil::insert(self::tblName(),$data);
    }

	public static function increUploadTimes($id = 0){
		if($id === 0){
			return;
		}
		DBUtil::increProp(self::tblName(),'upload_times','user_id = '.$id);
	}
		
	public static function userDetail(){
		$sql = 'select user_pic,disk_size,disk_used,upload_times ';
		$sql .= self::from_where(array('user_id = '.User::id()));
		$user =  DBUtil::queryRow($sql);
		if(!$user){
			return $user;
		}
		$user['user_id'] = User::id();
		$user['user_name'] = User::name();
		if($user['user_pic']==''){
			$user['user_pic']='/images/no_user_pic.png';
		}
		$user['disk_size_str'] = FileUtil::sizeStr($user['disk_size']);
		$user['disk_used_str'] = FileUtil::sizeStr($user['disk_used']);
		$user['width'] = ($user['disk_used'] / $user['disk_size'])*100;
		
		if(DiskGenAR::isLoginToday(User::id())){
			$user['sign_in'] = 1;
		}else{
			$user['sign_in'] = 0;
		}
		return $user;
	}
	
	private static function from_where($condition = array()){
		$condition[] = 'is_legal=1';
		$sql = DBUtil::buildFromSingleTbl(self::tblName());
		$sql .= DBUtil::buildWhere($condition);
		return $sql;
	}
	
    public static function increDiskSize($userId = 0,$genSize = 0){
        if($userId === 0){
            return;
        }
        $sql = 'update '.self::tblName().' set disk_size = disk_size + '.$genSize.' where user_id = '.$userId;
        DBUtil::exec($sql);
    }
		
	public static function increDiskUsedAndDiskSize($userId,$bookSize){
		$sql = 'update '.self::tblName().' set disk_size = disk_size + '.$bookSize.',disk_used = disk_used+'.$bookSize.' where user_id = '.$userId;
		DBUtil::exec($sql);
	}
}
